import axios from 'axios'
import { showToast } from 'vant'

//1. 创建axios 的请求实例对象
const http = axios.create({
  baseURL: 'https://meikou-api.itheima.net/',
  timeout: 50000
})

//2. 配置请求拦截器
http.interceptors.request.use((config) => {
    try {
      const user = mk.queryUser()
      if (user.token) {
        config.headers.Authorization = `Bearer ${user.token}`
      }
    } catch (e) {
      showToast({ message: '美蔻SDK异常' })
    }
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

//3. 配置响应拦截器
http.interceptors.response.use((res)=>{
    return res;
},(error)=>{
    return Promise.reject(error);
})

//4.导出实例对象
export { http }